﻿package com.moonshineproject.plugin.globalerrorhandling {
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	public class GEHButton extends Sprite {
		private static const BUTTON_HEIGHT:Number = 30;
		
		private static const NORMAL:int = 0;
		private static const MOUSE_DOWN:int = 1;
		
		private var labelTxt:TextField = new TextField();
		public function GEHButton(label:String) {
			labelTxt.defaultTextFormat = new TextFormat("Arial", 12, 0x5D5D5D);
			labelTxt.selectable = false;
			labelTxt.text = label;
			labelTxt.autoSize = TextFieldAutoSize.LEFT;
			labelTxt.x = 4;
			labelTxt.y = BUTTON_HEIGHT/2-labelTxt.height/2;
			addChild(labelTxt);
			
			buttonMode = true;
			mouseChildren = false;
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
			
			redraw();
		}
		private function mouseDown(e:MouseEvent):void {
			redraw(MOUSE_DOWN);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp, false, 0, true);
		}
		private function mouseUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			redraw();
		}
		private function redraw(state:int = NORMAL):void {
			var w:Number = labelTxt.width+labelTxt.x*2;
			var h:Number = BUTTON_HEIGHT;
			
			var g:Graphics;
			var m:Matrix;
			
			switch (state) {
				case MOUSE_DOWN:	labelTxt.textColor = 0x727272; break;
				default: 			labelTxt.textColor = 0x5D5D5D;
			}
			
			g = graphics;
			g.clear();
			m = new Matrix();
			m.createGradientBox(w, h, Math.PI*0.5);
			g.lineStyle(1, 0xCACACA, 1, true);
			
			switch (state) {
				case MOUSE_DOWN:	g.beginGradientFill(GradientType.LINEAR, [0xD4D4D4, 0xE6E6E6], [1, 1], [0, 255], m); break;
				default:			g.beginGradientFill(GradientType.LINEAR, [0xE6E6E6, 0xD4D4D4], [1, 1], [0, 255], m);
			}
			
			g.drawRoundRectComplex(0, 0, w, h, 6, 6, 6, 6);
			g.endFill();
		}
	}
}